package com.five.controller;

import com.alibaba.fastjson.JSON;
import com.five.entity.Found;
import com.five.service.FileGroupService;
import com.five.service.FoundService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zero on 2018/3/5.
 */
@RestController
@RequestMapping("/youxue/found")
public class FoundController {

    private static final Logger looger = Logger.getLogger(BusinessActivitiesController.class);

    @Autowired
    private FoundService foundService;
    @Autowired
    private FileGroupService fileGroupService;

    /**
     * 查找不同类型的“发现”信息
     * @return
     */
    @RequestMapping(value = "/content/{foundType}",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String getCampActivities(@PathVariable String foundType){

        Map<String,Object> map= new HashMap<String, Object>();
        List<Found> founds=foundService.findFoundByType(foundType);
        if(founds!=null){
            for(int i=0;i<founds.size();i++){
                String img=fileGroupService.findFileGroupById(founds.get(i).getFileGroupId()).getGroupBaseUrl();
                founds.get(i).setImg(img);                               //图片
            }
            return JSON.toJSON(founds).toString();
        }else {
            return "error";
        }
    }


    /**
     * 查询单个“发现”详情
     * @return
     */
    @RequestMapping(value = "/select/{foundId}",method = RequestMethod.GET,produces ="text/plain;charset=UTF-8")
    public String selectFound(@PathVariable int foundId) {

        Found found=foundService.findFoundById(foundId);
        if (found != null) {
            String img=fileGroupService.findFileGroupById(found.getFileGroupId()).getGroupBaseUrl();
            found.setImg(img);
            return JSON.toJSON(found).toString();

        } else {
            return "error";
        }

    }


}